<template>
    <div class="comment-container">
        <div>发表评论</div>
        <hr>
        <textarea v-model="msg" placeholder="请输入您的神评（最多吐槽120字哟）"></textarea>
        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <div class="comment-area">
            <div class="comment-list" v-for="(item,i) in comments" :key="i">
                <div class="comment-title">{{ i + 1 }}楼&nbsp;&nbsp;
                    用户：{{ item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time | dateFormat}}</div>
                <p class="comment-content">{{ item.content}}</p>
            </div>
        </div>
        <mt-button size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui'

    export default {
        data() {
            return {
                pageIndex: 1,
                comments: [],
                msg: ''
            }
        },
        created() {
            this.getComments()
        },
        methods: {
            getComments() { //默认加载第一页评论列表
                this.$http.get('api/getcomments/' + this.newsId + '?pageindex=' + this.pageIndex)
                    .then(result => {
                        if (result.body.status === 0) {
                            if (result.body.message.length === 0) {
                                Toast("没有更多评论了")
                            } else {

                                this.comments = this.comments.concat(result.body.message)
                            }
                        } else {
                            Toast("获取评论失败！")
                        }
                    })
            },
            getMore(){ //加载更多评论
                this.pageIndex++
                this.getComments()
            },
            postComment(){
                if (this.msg.trim().length === 0){
                    return Toast('评论内容不能为空！')
                }
                this.$http.post(`api/postcomment/${this.newsId}`, {
                    content: this.msg.trim()
                }).then(result => {
                    if (result.body.status === 0){
                        this.comments.unshift({
                            user_name: "匿名用户",
                            add_time: Date.now(),
                            content: this.msg
                        })
                        this.msg = ''
                    }
                })
            }

        },
        props: ['newsId']
    }
</script>

<style lang="scss" scoped>
    .comment-container {
        textarea {
            margin: 0;
            height: 85px;
            font-size: 16px;
        }
        .comment-area {
            .comment-list {
                .comment-title {
                    margin: 5px 0;
                    background-color: gray;
                    font-size: 13px;
                    line-height: 30px;
                }
                .comment-content {
                    font-size: 13px;
                    padding: 5px 0;
                    text-indent: 2em;
                }
            }
        }
    }
</style>